/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.util.Random;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author 11
 */
public class XMLWriter {

    public static void main(String[] args) throws IOException, ParserConfigurationException, TransformerConfigurationException, TransformerException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

        Document doc = documentBuilder.newDocument();
        Element bd = doc.createElement("citiesanddistances");
        doc.appendChild(bd);       
        
        Element cities = doc.createElement("cities");
        Element distances = doc.createElement("distancetables");
        bd.appendChild(cities);
        bd.appendChild(distances);
        
        double latitude = -89.99;
        double longitude = 0.0;

        for (int i = 0; i < 10000; ++i) {
            Element city = doc.createElement("city");
            cities.appendChild(city);

            Element nameCity = doc.createElement("name");
            nameCity.appendChild(doc.createTextNode("city" + i));
            city.appendChild(nameCity);

            Element latitudeCity = doc.createElement("latitude");
            latitudeCity.appendChild(doc.createTextNode("" + latitude));
            city.appendChild(latitudeCity);

            Element longitudeCity = doc.createElement("longitude");
            longitudeCity.appendChild(doc.createTextNode("" + longitude));
            city.appendChild(longitudeCity);

            latitude += 0.01;
            longitude += 0.01;
        }

        for (int i = 0; i < 100; i++) {
            for (int j = 1000; j < 2000; j++) {
                Element distancetable = doc.createElement("distancetable");
                distances.appendChild(distancetable);

                Element fromcity = doc.createElement("fromcity");
                fromcity.appendChild(doc.createTextNode("city" + i));
                distancetable.appendChild(fromcity);

                Element tocity = doc.createElement("tocity");
                tocity.appendChild(doc.createTextNode("city" + j));
                distancetable.appendChild(tocity);

                Element distance = doc.createElement("distance");
                distance.appendChild(doc.createTextNode("" + new Random().nextDouble() * 1000));
                distancetable.appendChild(distance);
            }
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(doc);
        StreamResult streamResult = new StreamResult(new File("D:\\CitiesAndDiatances.xml"));
        transformer.transform(domSource, streamResult);
    }
}
